//
//  TTIHomeVC.m
//  ContentApp
//
//  Created by apple on 16/8/1.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import "TTIHomeVC.h"
#import "SDCycleScrollView.h"
#import "AppMacro.h"
#import "TTISearchVC.h"
#import "UIButton+Utils.h"
#import "TTICategoryVC.h"
#import "TTIChannelVC.h"
#import "TTISearchVC.h"

@interface TTIHomeVC ()<UISearchBarDelegate>
{
    UISearchBar *customSearchBar;
    UIButton    *categoryBtn;
    UIButton    *messageBtn;
}
@end

@implementation TTIHomeVC
- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}


#pragma mark - view life cycle

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NAV_IMAGEANDTEXT(self, @"", @"分类", img(@"top_icon_category"), @selector(gotoCategoryView), @"消息", img(@"top_icon_message"), @selector(gotoMessageView));

    [self initSearchBar];
//    [self initTopButton];
    
}

- (void)initSearchBar{
    UIView *view = [[UIView alloc] init];
    if (iPhone6P)
    {
        view.size = CGSizeMake(UIScreenWidth - 155, 30);
    }
    else
    {
        view.size = CGSizeMake(UIScreenWidth - 145, 30);
    }
    
    UIImageView *bgimageview = [[UIImageView alloc] init];
    bgimageview.frame = CGRectMake(-10, 0, view.width + 20, view.height);
    bgimageview.image = [UIImage imageNamed:@"top_bg_search"];
    [view addSubview:bgimageview];
    
    // 搜索图标
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(15, 30 / 2 - 7.5, 15, 15);
    imageView.image = [UIImage imageNamed:@"top_icon_search"];
    [bgimageview addSubview:imageView];
    
    // 请输入搜索文字
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(37, 0, 200, 30);
    label.text = @"请输入关键字";
    label.textColor = [UIColor colorForHex:@"B5B4B6"];
    label.font = [UIFont systemFontOfSize:13];
    [bgimageview addSubview:label];
    
    // 添加按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    [button addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    self.navigationItem.titleView = view;
}

#pragma  mark --- 初始化顶部导航栏左右侧按钮
- (void)initTopButton
{
    categoryBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [categoryBtn setImage:img(@"top_icon_category") forState:0];
    [categoryBtn setTitle:@"分类" forState:0];
    [categoryBtn setTitleColor:[UIColor colorForHex:@"1296db"] forState:0];
    [categoryBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [categoryBtn initButton];
    categoryBtn.backgroundColor = [UIColor redColor];
    [categoryBtn addTarget:self action:@selector(gotoCategoryView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc]initWithCustomView:categoryBtn];
    
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    messageBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [messageBtn setImage:img(@"top_icon_message") forState:0];
    [messageBtn setTitle:@"消息" forState:0];
    [messageBtn setTitleColor:[UIColor colorForHex:@"1296db"] forState:0];
    [messageBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [messageBtn initButton];
    [messageBtn addTarget:self action:@selector(gotoMessageView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:messageBtn];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)searchBtnClick
{
    TTISearchVC *searchVC = ViewControllerWithNameAndIdentifier(@"TTIHome", @"TTISearchVC");
    searchVC.hidesBottomBarWhenPushed = YES;//TTINavigationController已经处理，可以不用设置
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)gotoCategoryView
{
    TTICategoryVC *CategoryVC = ViewControllerWithNameAndIdentifier(@"TTICategoryVC", @"TTICategoryVC");
    [self.navigationController pushViewController:CategoryVC animated:YES];
}

- (void)gotoMessageView
{
    TTIChannelVC *ChannelVC = ViewControllerWithNameAndIdentifier(@"TTIHome", @"TTIChannelVC");
    [self.navigationController pushViewController:ChannelVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
